package model

import "go-devops/dao"

type User struct {
	Id         int64  `json:"id" gorm:"primaryKey"`
	Username   string `json:"username"`
	Password   string `json:"password"`
	Status     int    `json:"status"`
	UpdateTime string `json:"updateTime"`
	CreateTime string `json:"createTime"`
}

func (User) TableName() string {
	return "user"
}

func QueryUserList() (userList []*User, err error) {
	if err = dao.DB.Find(&userList).Error; err != nil {
		return nil, err
	}
	return userList, nil
}

func AddUser(user *User) error {
	return dao.DB.Create(user).Error
}

func GetUser(username string) (user *User, err error) {
	if err = dao.DB.Where("username = ?", username).First(&user).Error; err != nil {
		return nil, err
	}
	return user, nil
}

func QueryUserCount(username string) (count int64, err error) {
	err = dao.DB.Model(&User{}).Where("username = ?", username).Count(&count).Error
	return count, err
}
